package com.dianping.cat.consumer.transaction.model.transform;

import com.dianping.cat.consumer.transaction.model.IVisitor;
import com.dianping.cat.consumer.transaction.model.entity.AllDuration;
import com.dianping.cat.consumer.transaction.model.entity.Duration;
import com.dianping.cat.consumer.transaction.model.entity.Graph;
import com.dianping.cat.consumer.transaction.model.entity.Graph2;
import com.dianping.cat.consumer.transaction.model.entity.GraphTrend;
import com.dianping.cat.consumer.transaction.model.entity.Machine;
import com.dianping.cat.consumer.transaction.model.entity.Range;
import com.dianping.cat.consumer.transaction.model.entity.Range2;
import com.dianping.cat.consumer.transaction.model.entity.StatusCode;
import com.dianping.cat.consumer.transaction.model.entity.TransactionName;
import com.dianping.cat.consumer.transaction.model.entity.TransactionReport;
import com.dianping.cat.consumer.transaction.model.entity.TransactionType;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;



public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  35 */     this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(TransactionReport transactionReport) {
/*  40 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  42 */     build(transactionReport, out);
/*  43 */     return out.toByteArray();
  }


/*  47 */   public static void build(TransactionReport transactionReport, OutputStream out) { transactionReport.accept(new DefaultNativeBuilder(out)); }



  public void visitAllDuration(AllDuration allDuration) {
/*  52 */     writeTag(1, 0);
/*  53 */     writeInt(allDuration.getValue());

/*  55 */     writeTag(2, 0);
/*  56 */     writeInt(allDuration.getCount());

/*  58 */     writeTag(63, 3);
  }


  public void visitDuration(Duration duration) {
/*  63 */     writeTag(1, 0);
/*  64 */     writeInt(duration.getValue());

/*  66 */     writeTag(2, 0);
/*  67 */     writeInt(duration.getCount());

/*  69 */     writeTag(63, 3);
  }


  public void visitGraph(Graph graph) {
/*  74 */     writeTag(1, 0);
/*  75 */     writeInt(graph.getDuration());

/*  77 */     writeTag(2, 0);
    writeString(graph.getSum());

/*  80 */     writeTag(3, 0);
/*  81 */     writeString(graph.getAvg());

/*  83 */     writeTag(4, 0);
/*  84 */     writeString(graph.getCount());

/*  86 */     writeTag(5, 0);
/*  87 */     writeString(graph.getFails());

/*  89 */     writeTag(63, 3);
  }


  public void visitGraph2(Graph2 graph2) {
/*  94 */     writeTag(1, 0);
/*  95 */     writeInt(graph2.getDuration());

/*  97 */     writeTag(2, 0);
/*  98 */     writeString(graph2.getSum());

/* 100 */     writeTag(3, 0);
/* 101 */     writeString(graph2.getAvg());

/* 103 */     writeTag(4, 0);
/* 104 */     writeString(graph2.getCount());

/* 106 */     writeTag(5, 0);
/* 107 */     writeString(graph2.getFails());

/* 109 */     writeTag(63, 3);
  }


  public void visitGraphTrend(GraphTrend graphTrend) {
/* 114 */     writeTag(1, 0);
/* 115 */     writeInt(graphTrend.getDuration());

/* 117 */     writeTag(2, 0);
/* 118 */     writeString(graphTrend.getSum());

/* 120 */     writeTag(3, 0);
/* 121 */     writeString(graphTrend.getAvg());

/* 123 */     writeTag(4, 0);
/* 124 */     writeString(graphTrend.getCount());

/* 126 */     writeTag(5, 0);
/* 127 */     writeString(graphTrend.getFails());

/* 129 */     writeTag(63, 3);
  }


  public void visitMachine(Machine machine) {
/* 134 */     if (machine.getIp() != null) {
/* 135 */       writeTag(1, 1);
/* 136 */       writeString(machine.getIp());
    }

/* 139 */     if (!machine.getTypes().isEmpty()) {
/* 140 */       writeTag(33, 2);
/* 141 */       writeInt(machine.getTypes().size());

/* 143 */       for (TransactionType type : machine.getTypes().values()) {
/* 144 */         type.accept(this.m_visitor);
      }
    }

/* 148 */     writeTag(63, 3);
  }


  public void visitName(TransactionName name) {
/* 153 */     if (name.getId() != null) {
/* 154 */       writeTag(1, 1);
/* 155 */       writeString(name.getId());
    }

/* 158 */     writeTag(2, 0);
/* 159 */     writeLong(name.getTotalCount());

/* 161 */     writeTag(3, 0);
/* 162 */     writeLong(name.getFailCount());

/* 164 */     writeTag(4, 0);
/* 165 */     writeDouble(name.getFailPercent());

/* 167 */     writeTag(5, 0);
/* 168 */     writeDouble(name.getMin());

/* 170 */     writeTag(6, 0);
/* 171 */     writeDouble(name.getMax());

/* 173 */     writeTag(7, 0);
/* 174 */     writeDouble(name.getAvg());

/* 176 */     writeTag(8, 0);
/* 177 */     writeDouble(name.getSum());

/* 179 */     writeTag(9, 0);
/* 180 */     writeDouble(name.getSum2());

/* 182 */     writeTag(10, 0);
/* 183 */     writeDouble(name.getStd());

/* 185 */     if (name.getSuccessMessageUrl() != null) {
/* 186 */       writeTag(11, 1);
/* 187 */       writeString(name.getSuccessMessageUrl());
    }

/* 190 */     if (name.getFailMessageUrl() != null) {
/* 191 */       writeTag(12, 1);
/* 192 */       writeString(name.getFailMessageUrl());
    }

/* 195 */     writeTag(13, 0);
/* 196 */     writeDouble(name.getTps());

/* 198 */     writeTag(14, 0);
/* 199 */     writeDouble(name.getLine95Value());

/* 201 */     writeTag(15, 0);
/* 202 */     writeDouble(name.getLine99Value());

/* 204 */     writeTag(16, 0);
/* 205 */     writeDouble(name.getLine999Value());

/* 207 */     writeTag(17, 0);
/* 208 */     writeDouble(name.getLine90Value());

/* 210 */     writeTag(18, 0);
/* 211 */     writeDouble(name.getLine50Value());

/* 213 */     writeTag(19, 0);
/* 214 */     writeDouble(name.getLine9999Value());

/* 216 */     if (name.getLongestMessageUrl() != null) {
/* 217 */       writeTag(20, 1);
/* 218 */       writeString(name.getLongestMessageUrl());
    }

/* 221 */     if (!name.getRanges().isEmpty()) {
/* 222 */       writeTag(33, 2);
/* 223 */       writeInt(name.getRanges().size());

/* 225 */       for (Range range : name.getRanges().values()) {
/* 226 */         range.accept(this.m_visitor);
      }
    }

/* 230 */     if (!name.getDurations().isEmpty()) {
/* 231 */       writeTag(34, 2);
/* 232 */       writeInt(name.getDurations().size());

/* 234 */       for (Duration duration : name.getDurations().values()) {
/* 235 */         duration.accept(this.m_visitor);
      }
    }

/* 239 */     if (!name.getGraphs().isEmpty()) {
/* 240 */       writeTag(35, 2);
/* 241 */       writeInt(name.getGraphs().size());

/* 243 */       for (Graph graph : name.getGraphs().values()) {
/* 244 */         graph.accept(this.m_visitor);
      }
    }

/* 248 */     if (name.getGraphTrend() != null) {
/* 249 */       writeTag(36, 1);
/* 250 */       name.getGraphTrend().accept(this.m_visitor);
    }

/* 253 */     if (!name.getStatusCodes().isEmpty()) {
/* 254 */       writeTag(37, 2);
/* 255 */       writeInt(name.getStatusCodes().size());

/* 257 */       for (StatusCode statusCode : name.getStatusCodes().values()) {
/* 258 */         statusCode.accept(this.m_visitor);
      }
    }

/* 262 */     writeTag(63, 3);
  }


  public void visitRange(Range range) {
/* 267 */     writeTag(1, 0);
/* 268 */     writeInt(range.getValue());

/* 270 */     writeTag(2, 0);
/* 271 */     writeInt(range.getCount());

/* 273 */     writeTag(3, 0);
/* 274 */     writeDouble(range.getSum());

/* 276 */     writeTag(4, 0);
/* 277 */     writeDouble(range.getAvg());

/* 279 */     writeTag(5, 0);
/* 280 */     writeInt(range.getFails());

/* 282 */     writeTag(6, 0);
/* 283 */     writeDouble(range.getMin());

/* 285 */     writeTag(7, 0);
/* 286 */     writeDouble(range.getMax());

/* 288 */     writeTag(8, 0);
/* 289 */     writeDouble(range.getLine95Value());

/* 291 */     writeTag(9, 0);
/* 292 */     writeDouble(range.getLine99Value());

/* 294 */     writeTag(10, 0);
/* 295 */     writeDouble(range.getLine999Value());

/* 297 */     writeTag(11, 0);
/* 298 */     writeDouble(range.getLine90Value());

/* 300 */     writeTag(12, 0);
/* 301 */     writeDouble(range.getLine50Value());

/* 303 */     writeTag(13, 0);
/* 304 */     writeDouble(range.getLine9999Value());

/* 306 */     writeTag(63, 3);
  }


  public void visitRange2(Range2 range2) {
/* 311 */     writeTag(1, 0);
/* 312 */     writeInt(range2.getValue());

/* 314 */     writeTag(2, 0);
/* 315 */     writeInt(range2.getCount());

/* 317 */     writeTag(3, 0);
/* 318 */     writeDouble(range2.getSum());

/* 320 */     writeTag(4, 0);
/* 321 */     writeDouble(range2.getAvg());

/* 323 */     writeTag(5, 0);
/* 324 */     writeInt(range2.getFails());

/* 326 */     writeTag(6, 0);
/* 327 */     writeDouble(range2.getMin());

/* 329 */     writeTag(7, 0);
/* 330 */     writeDouble(range2.getMax());

/* 332 */     writeTag(8, 0);
/* 333 */     writeDouble(range2.getLine95Value());

/* 335 */     writeTag(9, 0);
/* 336 */     writeDouble(range2.getLine99Value());

/* 338 */     writeTag(10, 0);
/* 339 */     writeDouble(range2.getLine999Value());

/* 341 */     writeTag(11, 0);
/* 342 */     writeDouble(range2.getLine90Value());

/* 344 */     writeTag(12, 0);
/* 345 */     writeDouble(range2.getLine50Value());

/* 347 */     writeTag(13, 0);
/* 348 */     writeDouble(range2.getLine9999Value());

/* 350 */     writeTag(63, 3);
  }


  public void visitStatusCode(StatusCode statusCode) {
/* 355 */     if (statusCode.getId() != null) {
/* 356 */       writeTag(1, 1);
/* 357 */       writeString(statusCode.getId());
    }

/* 360 */     writeTag(2, 0);
/* 361 */     writeLong(statusCode.getCount());

/* 363 */     writeTag(63, 3);
  }


  public void visitTransactionReport(TransactionReport transactionReport) {
/* 368 */     writeTag(63, 0);

/* 370 */     if (transactionReport.getDomain() != null) {
/* 371 */       writeTag(1, 1);
/* 372 */       writeString(transactionReport.getDomain());
    }

/* 375 */     if (transactionReport.getStartTime() != null) {
/* 376 */       writeTag(2, 1);
/* 377 */       writeDate(transactionReport.getStartTime());
    }

/* 380 */     if (transactionReport.getEndTime() != null) {
/* 381 */       writeTag(3, 1);
/* 382 */       writeDate(transactionReport.getEndTime());
    }

/* 385 */     if (transactionReport.getDomainNames() != null) {
/* 386 */       writeTag(4, 2);
/* 387 */       writeInt(transactionReport.getDomainNames().size());

/* 389 */       for (String domain : transactionReport.getDomainNames()) {
/* 390 */         writeString(domain);
      }
    }

/* 394 */     if (transactionReport.getIps() != null) {
/* 395 */       writeTag(5, 2);
/* 396 */       writeInt(transactionReport.getIps().size());

/* 398 */       for (String ip : transactionReport.getIps()) {
/* 399 */         writeString(ip);
      }
    }

/* 403 */     if (!transactionReport.getMachines().isEmpty()) {
/* 404 */       writeTag(33, 2);
/* 405 */       writeInt(transactionReport.getMachines().size());

/* 407 */       for (Machine machine : transactionReport.getMachines().values()) {
/* 408 */         machine.accept(this.m_visitor);
      }
    }

/* 412 */     writeTag(63, 3);
  }


  public void visitType(TransactionType type) {
/* 417 */     if (type.getId() != null) {
/* 418 */       writeTag(1, 1);
/* 419 */       writeString(type.getId());
    }

/* 422 */     writeTag(2, 0);
/* 423 */     writeLong(type.getTotalCount());

/* 425 */     writeTag(3, 0);
/* 426 */     writeLong(type.getFailCount());

/* 428 */     writeTag(4, 0);
/* 429 */     writeDouble(type.getFailPercent());

/* 431 */     writeTag(5, 0);
/* 432 */     writeDouble(type.getMin());

/* 434 */     writeTag(6, 0);
/* 435 */     writeDouble(type.getMax());

/* 437 */     writeTag(7, 0);
/* 438 */     writeDouble(type.getAvg());

/* 440 */     writeTag(8, 0);
/* 441 */     writeDouble(type.getSum());

/* 443 */     writeTag(9, 0);
/* 444 */     writeDouble(type.getSum2());

/* 446 */     writeTag(10, 0);
/* 447 */     writeDouble(type.getStd());

/* 449 */     if (type.getSuccessMessageUrl() != null) {
/* 450 */       writeTag(11, 1);
/* 451 */       writeString(type.getSuccessMessageUrl());
    }

/* 454 */     if (type.getFailMessageUrl() != null) {
/* 455 */       writeTag(12, 1);
/* 456 */       writeString(type.getFailMessageUrl());
    }

/* 459 */     writeTag(13, 0);
/* 460 */     writeDouble(type.getTps());

/* 462 */     writeTag(14, 0);
/* 463 */     writeDouble(type.getLine95Value());

/* 465 */     writeTag(15, 0);
/* 466 */     writeDouble(type.getLine99Value());

/* 468 */     writeTag(16, 0);
/* 469 */     writeDouble(type.getLine999Value());

/* 471 */     writeTag(17, 0);
/* 472 */     writeDouble(type.getLine90Value());

/* 474 */     writeTag(18, 0);
/* 475 */     writeDouble(type.getLine50Value());

/* 477 */     writeTag(19, 0);
/* 478 */     writeDouble(type.getLine9999Value());

/* 480 */     if (type.getLongestMessageUrl() != null) {
/* 481 */       writeTag(20, 1);
/* 482 */       writeString(type.getLongestMessageUrl());
    }

/* 485 */     if (!type.getNames().isEmpty()) {
/* 486 */       writeTag(33, 2);
/* 487 */       writeInt(type.getNames().size());

/* 489 */       for (TransactionName name : type.getNames().values()) {
/* 490 */         name.accept(this.m_visitor);
      }
    }

/* 494 */     if (!type.getGraph2s().isEmpty()) {
/* 495 */       writeTag(34, 2);
/* 496 */       writeInt(type.getGraph2s().size());

/* 498 */       for (Graph2 graph2 : type.getGraph2s().values()) {
/* 499 */         graph2.accept(this.m_visitor);
      }
    }

/* 503 */     if (type.getGraphTrend() != null) {
/* 504 */       writeTag(35, 1);
/* 505 */       type.getGraphTrend().accept(this.m_visitor);
    }

/* 508 */     if (!type.getRange2s().isEmpty()) {
/* 509 */       writeTag(36, 2);
/* 510 */       writeInt(type.getRange2s().size());

/* 512 */       for (Range2 range2 : type.getRange2s().values()) {
/* 513 */         range2.accept(this.m_visitor);
      }
    }

/* 517 */     if (!type.getDynamicAttributes().isEmpty()) {
/* 518 */       writeTag(63, 2);
/* 519 */       writeInt(type.getDynamicAttributes().size());

/* 521 */       for (Map.Entry<String, String> dynamicAttribute : type.getDynamicAttributes().entrySet()) {
/* 522 */         writeString((String)dynamicAttribute.getKey());
/* 523 */         writeString((String)dynamicAttribute.getValue());
      }
    }

/* 527 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 532 */       writeVarint(value.getTime());
/* 533 */     } catch (Exception e) {
/* 534 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 540 */       writeVarint(Double.doubleToLongBits(value));
/* 541 */     } catch (Exception e) {
/* 542 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 548 */       writeVarint(value & 0xFFFFFFFFL);
/* 549 */     } catch (Exception e) {
/* 550 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 556 */       writeVarint(value);
/* 557 */     } catch (Exception e) {
/* 558 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 564 */       this.m_out.writeUTF(value);
/* 565 */     } catch (IOException e) {
/* 566 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 572 */       this.m_out.writeByte((field << 2) + type);
/* 573 */     } catch (IOException e) {
/* 574 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 580 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 581 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 584 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 585 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/transaction/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */